/*
 * @description: 
 * @Date: 2023-04-07 09:54:46
 * @example: 
 * @params: 
 */
const express = require("express");
const db = require("../db/config");
const bodyParser = require("body-parser");
const Layout = express.Router();

Layout.use(bodyParser.json());
Layout.use(express.urlencoded({ extended: true }));
Layout.use(express.static(__dirname + '/www'));


Layout.all("*", function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Headers", "Content-Type");
    res.header("Access-Control-Allow-Headers", "Authorization");
    next();
  });


/**
 * 布局信息列表
 * @route GET /LayoutList
 * @group ComponentLayout - 拖拽组件布局信息
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
Layout.get("/LayoutList",(req,res)=>{
    const sql = `SELECT * FROM WidgetInfo`
    db.query(sql,(data)=>{
        if(data){
            res.json({isSuccess:true,data,Code:200,msg:"获取列表成功!"})
        }else{
            res.json({isSuccess:false,data:[],Code:100,msg:"获取列表失败!"})
        }
    })
})


/**
 * 修改布局位置
 * @route GET /UpdateLayout
 * @group ComponentLayout - 拖拽组件布局信息
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
Layout.get("/UpdateLayout",(req,res)=>{
    const NewVal = req.query.params
    db.update("WidgetInfo",{ w:NewVal.w, x:NewVal.x, y:NewVal.y, h:NewVal.h},{Widget_id:NewVal.Widget_id},(data)=>{
        if (data.affectedRows > 0) {
            res.json({isSuccess:true,Code:200,msg:"修改成功!"})
          } else {
            res.json({isSuccess:false,data:[],Code:100,msg:"修改失败!"})
          }
    })
    
})

/**
 * 修改模块配置
 * @route GET /EditLayout
 * @group ComponentLayout - 拖拽组件布局信息
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
Layout.post("/EditLayout",(req,res)=>{
    const Widget_id  = req.query.id 
    const Widget_config = req.query.config
    db.update("WidgetInfo",{Widget_config},{Widget_id},(data)=>{
        if (data.affectedRows > 0) {
            res.json({isSuccess:true,Code:200,msg:"修改成功!"})
          } else {
            res.json({isSuccess:false,data:[],Code:100,msg:"修改失败!"})
          }
    })
})


/**
 * 添加布局模块信息
 * @route post /UpdateLayout
 * @group ComponentLayout - 拖拽组件布局信息
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
Layout.post('/AddLayoutComs',(req,res)=>{
    const NewVal = req.query;
    const {  Widget_title, type , Widget_config , x, y, h, i,w }  = NewVal
    db.insert('WidgetInfo',{Widget_title,type,Widget_config,x,y,h,w,i},(data)=>{
        if(data){
            res.json({isSuccess:true,Code:200,msg:"添加成功!"})
        }else {
            res.json({isSuccess:false,data:[],Code:100,msg:"error"})
        }
    }) 
})

/**
 * 删除布局模块信息
 * @route get /DelLayoutComs
 * @group ComponentLayout - 拖拽组件布局信息
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
Layout.get('/DelLayoutComs',(req,res)=>{
    const id = req.query.id
    db.del('WidgetInfo',{Widget_id:id},(data)=>{
        if(data){
            res.json({isSuccess:true,Code:200,msg:"删除成功!"})
        }else{
            res.json({isSuccess:false,data:[],Code:100,msg:"error"})
        }
    })
})

module.exports = Layout